#!/bin/bash
if [ $# -eq 0 ] && [ $# -lt 3 ]; then
	echo "Use $0 date month year"
	exit 0
fi
isOk=true
date=`echo $1 | grep -P "^\d{1,2}$"`
if [  $? -ne 0 ] || [ $date -lt 0 ];then
	echo "Ngay nhap phai co 1 hoac 2 so"
	exit 0
fi
mon=`echo "$2" | grep -P "^\d{1,2}$"`
if [ $? -ne 0 ] || [ $mon -gt 12 ]  || [ $mon -lt 0 ];then
	echo "Thang nhap phai co 1 hoac 2 so"
	exit 0
fi
year=`echo $3 | grep -P "^\d{4}$"`
if [ $? -ne 0 ] || [ $year -lt 0 ];then
	echo "Nam nhap phai co 4 so"
	exit 0
fi
isOk=true
case $mon in
	1|3|5|7|8|10|12) 
		if [ $date -gt 31 ]; then 
			isOk=false
			echo "So ngay ko hop le"
		fi;;
	2)
		isNhuan=false
		if [ $(($year % 4)) -eq 0 ]; then
			isNhuan=true
			if [ $(($year % 100)) -eq 0 ] && [ $(($year % 400)) -ne 0 ] ; then 
				isNhuan=false
			fi
		fi
		if [ $date -ne 29 ] && [ $isNhuan = true ]; then 
			isOk=false
			echo "So ngay ko hop le"
		elif [ $date -ne 28 ] && [ $isNhuan = false ]; then
			isOk=false;
			echo "So ngay ko hop le"
		fi;;
	4,|6|9|11) 
	if [ $date -gt 30 ]; then 
		isOk=false;
		echo "So ngay ko hop le"
	fi;;
esac
if [ $isOk = true ];then
	echo "ngay thang nhap vao hop le"
else
	echo "ngay thang nhap vao ko hop le"
fi
